package com.qiangang.yan.tools.util;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description:LocalDateTime日期工具类
 * @Author: qiangang.yan
 * @CreateDate: 2019/7/25 16:09
 **/
public class LocalDateTimeUtils {
    /**
     * 公用格式话日期格式和转换
     *
     * @param time   日期字符串
     * @param format 格式化如:yyyyMMddHHmmss
     */
    public static LocalDateTime format(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);

    }

    /**
     * 获取两个时间相差天数、小时、分数、秒
     *
     * @param rentTime
     * @param revertTime
     * @return
     */
    public static Duration duration(String rentTime, String revertTime, String format) {
        LocalDateTime orderStartTime = format(rentTime, format);
        LocalDateTime orderEndtimeTime = format(revertTime, format);
        Duration duration = Duration.between(orderStartTime, orderEndtimeTime);
        return duration;
    }


    /**
     * 当前传进来的日期的基础上加*天
     */
    public static LocalDateTime plusDays(String rentTime, Integer days, String format) {
        LocalDateTime currentDate = format(rentTime, format);
        currentDate.plusDays(days);
        return currentDate;
    }

    /**
     * 当前传进来的日期的基础上减*天
     */
    public static LocalDateTime minusDays(String rentTime, Integer days, String format) {
        LocalDateTime currentDate = format(rentTime, format);
        currentDate.minusDays(days);
        return currentDate;
    }

    /**
     * 当前传进来的日期的基础上加*小时
     */
    public static LocalDateTime plusHours(String rentTime, Integer huors, String format) {
        LocalDateTime currentDate = format(rentTime, format);
        currentDate.plusHours(huors);
        return currentDate;
    }

    /**
     * 当前传进来的日期的基础上减*小时
     */
    public static LocalDateTime minusHours(String rentTime, Integer huors, String format) {
        LocalDateTime currentDate = format(rentTime, format);
        currentDate.minusHours(huors);
        return currentDate;
    }

    /**
     * 将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区
     */
    public static LocalDateTime dateConvertToLocalDateTime(Date date) {
        return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
    }


    /**
     * 将java8 的 java.time.LocalDateTime 转换为 java.util.Date，默认时区为东8区
     */
    public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
    }

    /**
     * 获取当前系统时间的秒数
     */
    public static Long getCrrentTimeSecond() {
        return LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 获取当前系统时间毫秒数
     */
    public static Long getCrrentTimeMill() {
        return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }


    public static String getLocalDateTime(LocalDateTime fromDateTime, String dateTimeFotmat) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(dateTimeFotmat);
        if (fromDateTime != null) {
            String localTime = fromDateTime.format(df);
            return localTime;
        }
        return null;
    }

    /**
     * 获取本周开始时间
     */
    public static String getWeekBeginTimeString(String format) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        int currentOrdinal = currentDateTime.getDayOfWeek().ordinal();
        LocalDateTime weekBeginDateTime = currentDateTime.minusDays(currentOrdinal).withHour(0).withMinute(0).withSecond(0).withNano(0);
        return getLocalDateTime(weekBeginDateTime, format);
    }

    /**
     * 获取本周结束时间字符串 2019-06-16 23:59:59
     */
    public static String getWeekEndTimeString(String fromat) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        int currentOrdinal = currentDateTime.getDayOfWeek().ordinal();
        LocalDateTime weekEndDateTime = currentDateTime.plusDays(6 - currentOrdinal).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        return getLocalDateTime(weekEndDateTime, fromat);
    }

    /**
     * 获取本周结束时间
     */
    public static LocalDateTime getWeekEndTime() {
        LocalDateTime currentDateTime = LocalDateTime.now();
        int currentOrdinal = currentDateTime.getDayOfWeek().ordinal();
        return currentDateTime.plusDays(6 - currentOrdinal).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }


}
